<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr,
		errorModal
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				title:'',
				instIdCache: 'aft-bx-afterSalesService-title2-instIdCacheKey',
				unitCache:'aft-bx-afterSalesService-title2-unitCacheKey',
				newProductCacheKey:'aft-bx-afterSalesService-title2-newProductCacheKey',
				repairCompleteTime:'',
			})
		},
		methods:{
			onSelectProductionDate(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				let title1 = _that.$refs.autoform.getEditorTitle("title2","productionDate")
				let title2 = _that.$refs.autoform.getEditorTitle("title2","guaranteeDate")
				if(formdata.guaranteeDate){
					if(moment(val).isAfter(formdata.guaranteeDate)){
						errorModal(_that,'提示信息',title1+"不能大于"+title2,()=>{
							_that.$refs.autoform.clear("title2","productionDate")
						})
					}
				}
			},
			onSelectGuaranteeDate(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				let title1 = _that.$refs.autoform.getEditorTitle("title2","productionDate")
				let title2 = _that.$refs.autoform.getEditorTitle("title2","guaranteeDate")
				if(formdata.productionDate){
					if(moment(val).isBefore(formdata.productionDate)){
						errorModal(_that,'提示信息',title1+"不能小于"+title2,()=>{
							_that.$refs.autoform.clear("title2","guaranteeDate")
						})
					}
				}
				
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						"dictSource/loadCombo",
						{
							sourceId:'283',
							categoryId:'6431'
						}
					),
					_that.om.request(
						"dictSource/loadCombo",
						{
							sourceId:'289',
							categoryId:'8893'
						}
					),
					_that.om.request(
						"dictSource/loadCombo",
						{
							sourceId:'289',
							categoryId:'8897'
						}
					),
					_that.om.request(
						"inventoryCategory/getExistingProductLineAssortment",
						{}
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title2","isGuaranteeId","data",configSelectData(res[0],'id','text'))
					_that.$refs.autoform.initSelectIndex("title2","isGuaranteeId")
					_that.$refs.autoform.changeEditor("title2","faultCauseId","data",configSelectData(res[1],'id','text'))
					_that.$refs.autoform.initSelectIndex("title2","faultCauseId")
					_that.$refs.autoform.changeEditor("title2","serviceResultsId","data",configSelectData(res[2],'id','text'))
					_that.$refs.autoform.initSelectIndex("title2","serviceResultsId")
					_that.$refs.autoform.changeEditor("title2","marketingPlanningProductId","data",configSelectData(res[3],'id','name'))
					_that.$refs.autoform.initSelectIndex("title2","marketingPlanningProductId")
					
					_that.$nextTick(function(){
						if(_that.$refs.autoform){
							if(_that.$refs.autoform.formdata.serviceResultsId == 7054){
								_that.$refs.autoform.changeEditor("title2","repairCompleteTime","required",true)
							}else{
								_that.$refs.autoform.changeEditor("title2","repairCompleteTime","required",false)
							}
							if(_that.repairCompleteTime){
								_that.$refs.autoform.formdata.repairCompleteTime = _that.repairCompleteTime
							}
						}
					})
				})
			},
			clearNewProductCode(){
				let _that = this
				_that.$refs.autoform.clear("title2",'newProductId')
				_that.$refs.autoform.clear("title2",'newProductName')
				_that.$refs.autoform.clear("title2",'customerProductName')
				_that.$refs.autoform.clear("title2",'newProductStd')
				_that.$refs.autoform.clear("title2",'productCategoryName')
				_that.$refs.autoform.clear("title2",'warrantyNum')
				_that.$refs.autoform.clear("title2",'productionDate')
				_that.$refs.autoform.clear("title2",'isGuaranteeId')
				_that.$refs.autoform.clear("title2",'guaranteeDate')
				_that.$refs.autoform.clear("title2",'customerProductStd')
				_that.$refs.autoform.clear("title2",'unitCode')
				_that.$refs.autoform.clear("title2",'marketingPlanningProductId')
				_that.$refs.autoform.clear("title2",'askDesperation')
				_that.$refs.autoform.clear("title2",'expectFinishDate')
				_that.$refs.autoform.clear("title2",'lateServiceTypeId')
				_that.$refs.autoform.clear("title2",'communicateRemark')
				_that.$refs.autoform.clear("title2",'instId')
				_that.$refs.autoform.clear("title2",'communicateResultId')
				_that.$refs.autoform.clear("title2",'remark')
				_that.$refs.autoform.clear("title2",'repairTaskNumber')
				_that.$refs.autoform.clear("title2",'afterApprovalNumber')
				_that.$refs.autoform.clear("title2",'afterSaveNumber')
				_that.$refs.autoform.clear("title2",'faultDescription')
				_that.$refs.autoform.clear("title2",'faultCauseId')
				_that.$refs.autoform.clear("title2",'troubleshootingMeasuresRemark')
				_that.$refs.autoform.clear("title2",'serviceResultsId')
				_that.$refs.autoform.clear("title2",'pendingMatters')
				_that.$refs.autoform.clear("title2",'serviceConfirmationNumber')
				_that.$refs.autoform.clear("title2",'repairCompleteTime')
				_that.$refs.autoform.clear("title2",'identificationCode')
				_that.$refs.autoform.clear("title2",'softwareVersion')
				_that.$refs.autoform.clear("title2",'batchNumber')
				_that.$refs.autoform.clear("title2",'localIP')
				_that.$refs.autoform.clear("title2",'serverIP')
				_that.$refs.autoform.clear("title2",'repairCustom1')
				_that.$refs.autoform.clear("title2",'repairCustom2')
				_that.$refs.autoform.clear("title2",'repairCustom3')
			},
			onselectServiceResults(item){
				let _that = this
				if(item.id == 7054){
					_that.$refs.autoform.changeEditor("title2","repairCompleteTime","required",true)
				}else{
					_that.$refs.autoform.changeEditor("title2","repairCompleteTime","required",false)
				}
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if(e.title){
					_that.title = e.title
				}
				if(e.repairCompleteTime){
					_that.repairCompleteTime = e.repairCompleteTime
				}
				
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							
							changeEditor('title2', "newProductId", {
								'link': '../../public/pageSearch/pageSearch',
								"linkParam": {
									url:'productShowSecond/getProductPage',
									namefield: 'projectCode',
									extrafield:'projectStd',
									notefield:'projectName',
									searchfield: "q",
									initload:true,
									cachename: _that.newProductCacheKey,
								}
							}, null, _that.autoformdata)
							
							changeEditor("title2","instId",{
								"link":'../../public/search/search',
								"linkParam":{
									other:JSON.stringify(),
									url:'tinstitution/getComboTree',
									namefield:'text',
									initload:true,
									cachename:_that.instIdCache
								}
							},null,_that.autoformdata)
							
							changeEditor("title2","unitCode",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({}),		
									url:'uomGroup/selectAllChildGrid',
									initload:true,
									searchfield:'q',
									namefield:'unitName',
									extrafield:'unitCode',
									notefield:'unitGroupName',
									cachename:_that.unitCache
								}
							},null,_that.autoformdata)
							if(!_that.formdata.repairCompleteTime){
								changeEditor("title2","repairCompleteTime","noInitDate",true,_that.autoformdata)
							}
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileSaveNameField", "fileSaveName")
							changeEditor(_that.autoformdata[0].editors, "fileList", "uploadUrl", "uploadFileData/uploadInterimFile")
							changeEditor(_that.autoformdata[0].editors, "fileList", "loadUrl", "uploadFileData/getInterimFile")
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", 150)
							if(_that.formdata.fileList) {
								var tempFileList = []
								var fileUrlList = _that.formdata.fileList.map(t=>{
									tempFileList.push(t.fileSaveName)
									return t.fileSaveName
								})
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
								changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
							}
							
							_that.initload()
						}
					})
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.instIdCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instId = d.id
							formdata.instName = d.text
							uni.removeStorage({
								key:_that.instIdCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.unitCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.unitCode = d.unitCode
							formdata.unitName = d.unitName
							uni.removeStorage({
								key:_that.unitCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.newProductCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.newProductCode = d.newProductCode
							formdata.newProductName = d.newProductName
							formdata.newProductStd = d.newProductStd
							formdata.productCategoryName = d.productCategoryName
							formdata.customerProductName = d.projectName
							formdata.customerProductStd = d.projectStd
							formdata.newProductId = d.id
							formdata.unitCode = d.unitCode
							formdata.unitName = d.unitName
							uni.removeStorage({
								key:_that.newProductCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
                                             